from airflow.patsnap.util.ding import Ding
from airflow.models.idata_alert import Alert
from airflow.models.idata_task_alert import TaskAlert
from airflow.models.idata_workflow_alert import WorkflowAlert
from airflow.utils.session import provide_session

alert_condition_map = {
    0: '执行失败',
    1: '调度延时',
    2: '执行延时'
}

alert_category_map = {
    0: '钉钉组',
    1: '电话',
    2: '邮件组',
}


@provide_session
def get_task_alerts_display(task_id, condition, session=None):
    data = {
        0: {'alert_id': None, 'category': 0, 'category_name': '钉钉组'}
    }
    alerts = session.query(Alert).join(TaskAlert,
                                       Alert.id == TaskAlert.alert_id).filter(TaskAlert.task_id == task_id,
                                                                              TaskAlert.condition == condition).all()
    for al in alerts:
        if al.category in data:
            data[al.category]['alert_id'] = al.id
    return [v for v in data.values()]


@provide_session
def get_task_alerts(task_id, condition, session=None):
    alerts = session.query(Alert).join(TaskAlert,
                                       Alert.id == TaskAlert.alert_id).filter(TaskAlert.task_id == task_id,
                                                                              TaskAlert.condition == condition).all()
    return alerts


@provide_session
def save_task_alerts(task_id, alerts, session=None):
    for alert in alerts:
        condition = int(alert['condition'])
        TaskAlert.delete_exist(task_id, condition=condition, session=session)
        if alert.get('alert_id', None):
            TaskAlert(task_id=task_id, alert_id=int(alert['alert_id']), condition=condition).insert(session=session)


@provide_session
def send_task_alerts(task_id, task_name, project_name, condition, session=None, msg=''):
    try:
        if str(task_id).find('-') > 0:
            task_id = int(task_id.split('-')[-1])
        for alert in get_task_alerts(task_id, condition, session=session):
            if alert.category == 0:
                text = '[精卫告警] 任务{} id:{},name:{},project_name:{},msg:{}'.format(alert_condition_map[condition], task_id, task_name, project_name, msg)
                Ding(alert.content, text).send()
    except Exception as e:
        print(e)


@provide_session
def get_workflow_alerts(workflow_id, condition, session=None):
    alerts = session.query(Alert).join(WorkflowAlert,
                                       Alert.id == WorkflowAlert.alert_id).filter(WorkflowAlert.workflow_id == workflow_id,
                                                                                  WorkflowAlert.condition == condition).all()
    return alerts


@provide_session
def save_workflow_alerts(workflow_id, alerts, condition=0, session=None):
    WorkflowAlert.delete_exist(workflow_id, condition=condition, session=session)
    for alert in alerts:
        if alert.get('alert_id', None):
            WorkflowAlert(workflow_id=workflow_id, alert_id=int(alert['alert_id']), condition=condition).insert(session=session)


@provide_session
def get_workflow_alerts_display(workflow_id, condition, session=None):
    data = {
        0: {'alert_id': None, 'category': 0, 'category_name': '钉钉组'}
    }
    alerts = session.query(Alert).join(WorkflowAlert,
                                       Alert.id == WorkflowAlert.alert_id).filter(WorkflowAlert.workflow_id == workflow_id,
                                                                                  WorkflowAlert.condition == condition).all()
    for al in alerts:
        if al.category in data:
            data[al.category]['alert_id'] = al.id
    return [v for v in data.values()]


